import Vue from 'vue'
import VueRouter from 'vue-router'

// 引入组件
// import Home from '../views/Home.vue'
import Login from '../views/users/Login.vue'
import Login1 from '../views/users/Login1.vue'
import Login2 from '../views/users/Login2.vue'
import Admin from '../views/admin/Admin.vue'
import Welcome from '../views/admin/Welcome.vue'
import GoodsIndex from '../views/goods/Goods.vue'
import GoodsCreate from '../views/goods/Create.vue'
import OrdersIndex from '../views/orders/Orders.vue'
import OrdersTotal from '../views/orders/total.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    component: Admin,
    children: [
      {path: 'welcome', alias: '/', component: Welcome},
      {path: 'goods', component: GoodsIndex},
      {path: 'goodsCreate', component: GoodsCreate},
      {path: 'orders', component: OrdersIndex},
      {path: 'ordersTotal', component: OrdersTotal},
    ]
  },
  {path: '/login', component: Login},
  {path: '/login1', component: Login1},
  {path: '/login2', component: Login2},
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  }
]


const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})

// 导航守卫
// next()                 让用户正常访问
// next({path: '/login'})  重定向到登录页
router.beforeEach((newData, oldData, next) => {
  // console.log('路由变化了！也就是你打开或刷新页面了', newData)
  // 判断用户是否登录
  // 不是登录页 && 检测不到TOKEN   next({path: '/login'}) 
  // 否则 next()

  // 不是登录页 && 检测不到TOKEN   next({path: '/login'}) 
  // 否则 next()

  let token = localStorage.getItem('token')
  // if (newData.path != '/login' && !token)   大家这么写没问题
  if (newData.path != '/login' && newData.path != '/login1' && newData.path != '/login2' && !token)
  {
    next({path: '/login'}) 
  } else {
    next()
  }

})

export default router
